import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {Buffer} from "buffer";
import {VISIBLESTRING4} from "./VISIBLESTRING4";
import {VISIBLESTRING6} from "./VISIBLESTRING6";
import {VISIBLESTRING8} from "./VISIBLESTRING8";


export class FactoryVersion implements AxdrType{

    dataCode : Buffer | null = null;
    fcode : VISIBLESTRING4 | null = null;
    sver : VISIBLESTRING4 | null = null;
    sverDate : VISIBLESTRING6 | null = null;
    hver : VISIBLESTRING4 | null = null;
    hverDate : VISIBLESTRING6 | null = null;
    fextend : VISIBLESTRING8 | null = null;

    constructor() {
    }

    set_dataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    set_all(fcode : VISIBLESTRING4, sver : VISIBLESTRING4, sverDate : VISIBLESTRING6, hver : VISIBLESTRING4, hverDate : VISIBLESTRING6, fextend : VISIBLESTRING8){
        this.fcode = fcode;
        this.sver = sver;
        this.sverDate = sverDate;
        this.hver = hver;
        this.hverDate = hverDate;
        this.fextend = fextend;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;

        this.fcode = new VISIBLESTRING4();
        codeLength += this.fcode.decode(input);

        this.sver = new VISIBLESTRING4();
        codeLength += this.sver.decode(input);

        this.sverDate = new VISIBLESTRING6();
        codeLength += this.sverDate.decode(input);

        this.hver = new VISIBLESTRING4();
        codeLength += this.hver.decode(input);

        this.hverDate = new VISIBLESTRING6();
        codeLength += this.hverDate.decode(input);

        this.fextend = new VISIBLESTRING8();
        codeLength += this.fextend.decode(input);

        return codeLength;
    }

    encode(output: ReverseByteArrayOutputStream): number {
        let codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if(this.fcode != null && this.sver != null && this.sverDate != null && this.hver != null && this.hverDate != null && this.fextend != null){
                codeLength = 0;
                codeLength += this.fextend.encode(output);
                codeLength += this.hverDate.encode(output);
                codeLength += this.hver.encode(output);
                codeLength += this.sverDate.encode(output);
                codeLength += this.sver.encode(output);
                codeLength += this.fcode.encode(output);
            }else{
                throw new Error("fextend || hverDate || hver || sverDate || sver || fcode is null");
            }
        }
        return codeLength;
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    toString(): string {
        if (this.fcode != null && this.sver != null && this.sverDate != null && this.hver != null && this.hverDate != null && this.fextend != null) {
            return "sequence: {"+ "fcode: " + this.fcode + ", sver: " + this.sver + ", sverdate: " + this.sverDate + ", hver: " + this.hver + ", hverdate: " + this.hverDate + ", fextend: " + this.fextend  + "}";
        } else {
            return "fextend || hverDate || hver || sverDate || sver || fcode is null";
        }
    }

}
